package org.gd.hellojaxb.example.map;

import org.gd.hellojaxb.privat24.other.SessionCounterDateList;

import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class MapAdaptersFactory {

    public static final class StringStringMapAdapter
            extends XmlAdapter<StringStringMapElements[], Map<String, String>> {

        public StringStringMapElements[] marshal(Map<String, String> arg0) throws Exception {
            StringStringMapElements[] mapElements = new StringStringMapElements[arg0.size()];
            int i = 0;
            for (Map.Entry<String, String> entry : arg0.entrySet()) {
                mapElements[i++] = new StringStringMapElements(entry.getKey(), entry.getValue());
            }
            return mapElements;
        }
        public Map<String, String> unmarshal(StringStringMapElements[] arg0) throws Exception {
            Map<String, String> r = new HashMap<String, String>();
            for (StringStringMapElements mapElements : arg0) {
                r.put(mapElements.getKey(), mapElements.getValue());
            }
            return r;
        }
    }

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlRootElement(name = "item")
    private static final class StringStringMapElements {
        @XmlAttribute
        private String  key;
        @XmlAttribute
        private String value;

        private StringStringMapElements() {
        }

        public StringStringMapElements(String key, String value) {
            this();
            this.key = key;
            this.value = value;
        }

        public String getKey() {
            return key;
        }

        public String getValue() {
            return value;
        }

        @Override
        public String toString() {
            return "StringStringMapElements{" +
                    "key='" + key + '\'' +
                    ", value='" + value + '\'' +
                    '}';
        }
    }

    public static final class SessionCounterDateListMapAdapter
            extends XmlAdapter<SessionCounterDateListMapElements[], Map<Integer, SessionCounterDateList>> {

        public SessionCounterDateListMapElements[] marshal(Map<Integer, SessionCounterDateList> arg0) throws Exception {
            SessionCounterDateListMapElements[] mapElements = new SessionCounterDateListMapElements[arg0.size()];
            int i = 0;
            for (Map.Entry<Integer, SessionCounterDateList> entry : arg0.entrySet()) {
                mapElements[i++] = new SessionCounterDateListMapElements(entry.getKey(), entry.getValue());
            }
            return mapElements;
        }

        public Map<Integer, SessionCounterDateList> unmarshal(SessionCounterDateListMapElements[] arg0) throws Exception {
            Map<Integer, SessionCounterDateList> r = new HashMap<Integer, SessionCounterDateList>();
            for (SessionCounterDateListMapElements mapElements : arg0) {
                r.put(mapElements.getKey(), mapElements.getValue());
            }
            return r;
        }
    }

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlRootElement(name = "item")
    private static final class SessionCounterDateListMapElements {
        @XmlAttribute
        private Integer  key;
        @XmlElement
        private SessionCounterDateList value;

        private SessionCounterDateListMapElements() {
        }

        private SessionCounterDateListMapElements(Integer key, SessionCounterDateList value) {
            this();
            this.key = key;
            this.value = value;
        }

        public Integer getKey() {
            return key;
        }

        public void setKey(Integer key) {
            this.key = key;
        }

        public SessionCounterDateList getValue() {
            return value;
        }

        public void setValue(SessionCounterDateList value) {
            this.value = value;
        }
    }
}
